<?php


namespace app\lmsx\admin;

use app\common\builder\ZBuilder;
use app\lmsx\model\Article as ArticleModel;
use app\lmsx\model\Category as CategoryModel;
use app\lmsx\model\Message as MessageModel;
use think\Db;

/**
 * 关于我们
 * @author lan
 */
class About extends Base
{
    public function index(){
        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();

            // 验证
            $result = $this->validate($data, 'Meal');
            // 验证失败 输出错误信息
            if(true !== $result) $this->error($result);

            //查询是否添加过数据
            $row=Db::table('dp_lmsx_article')->where(['ident'=>'about'])->find();

            if (empty($row)){
                if ($res=ArticleModel::create($data)) {//添加
                    $this->success('保存成功', url('index'));
                } else {
                    $this->error('保存失败');
                }
            }else{//修改
                if (false!==ArticleModel::update($data)) {
                    $this->success('保存成功', url('index'));
                } else {
                    $this->error('保存失败');
                }
            }
        }

        // 获取数据,第一次添加时没有数据赋值表单内容为空数组,标识xsyd
        $info = Db::table('dp_lmsx_article')->where(['ident'=>'about'])->find();


        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('关于我们') // 设置页面标题
            ->addFormItems([ // 批量添加表单项
                ['hidden','ident','about'],
                ['hidden','id',10],
                ['ueditor', 'content', '内容', '必填'],
            ])
            ->setFormData($info) // 设置表单数据
            ->setBtnTitle('submit', '保存')
            ->hideBtn('back')
            ->fetch();
    }

}